// ==================== 1. 单程航班信息 ====================
package org.chen.common.flight;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import org.springframework.util.StringUtils;

import javax.validation.constraints.Pattern;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class OneWayFlightInfo {
    private Long id;
    // 基础信息
    private String itineraryId;
    private Integer priceIndex;

    private String flightNumber;           // 航班号（多段用+连接）

    private String airlineCode;            // 主要航空公司代码

    private String airline;                // 主要航空公司名称

    // 时间信息
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime departureTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime arrivalTime;

    private String duration;

    private Integer crossDays;
    
    // 机场信息
    private String departureAirportCode;
    private String arrivalAirportCode;
    private String departureCityCode;
    private String arrivalCityCode;

    private String departureAirport;

    private String arrivalAirport;

    private String departureCity;

    private String arrivalCity;

    private String departureTerminal;
    private String arrivalTerminal;
    
    // 价格信息
    private BigDecimal adultPrice;         // 成人基础价格
    private BigDecimal adultTax;           // 成人税费
    private BigDecimal adultTotalPrice;    // 成人总价
    private BigDecimal childPrice;         // 儿童基础价格
    private BigDecimal childTax;           // 儿童税费
    private BigDecimal childTotalPrice;    // 儿童总价
    private BigDecimal infantPrice;        // 婴儿基础价格
    private BigDecimal infantTax;          // 婴儿税费
    private BigDecimal infantTotalPrice;   // 婴儿总价
    private String currency;
    private Integer miseryIndex;
    private Integer ticketCount;
    
    // 航班特征
    private Integer transferCount;         // 中转次数
    private Boolean isDirectFlight;        // 是否直飞
    private String cabinClass;             // 舱位

    private String baggageInfo;            // 行李信息

    private List<String> purchaseNotes;    // 购买须知
    
    // 航段详情
    private List<FlightSegmentDetail> segments;  // 航段列表
    private List<String> transferWaitTimes;      // 中转等待时间
    
    // 系统信息
    private LocalDateTime lastUpdated;
    
    public OneWayFlightInfo() {
        this.currency = "CNY";
        this.transferCount = 0;
        this.isDirectFlight = true;
        this.lastUpdated = LocalDateTime.now();
    }
    
    // 计算总价
    public BigDecimal getAdultTotalPrice() {
        if (adultPrice != null && adultTax != null) {
            return adultPrice.add(adultTax);
        }
        return BigDecimal.ZERO;
    }
    
    public BigDecimal getChildTotalPrice() {
        if (childPrice != null && childTax != null) {
            return childPrice.add(childTax);
        }
        return BigDecimal.ZERO;
    }
    
    public BigDecimal getInfantTotalPrice() {
        if (infantPrice != null && infantTax != null) {
            return infantPrice.add(infantTax);
        }
        return BigDecimal.ZERO;
    }

    public void setTimesFromString(String departureDateTimeStr, String arrivalDateTimeStr) {
        // 自定义解析器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 使用 DateTimeFormatter 解析日期字符串
        this.departureTime = LocalDateTime.parse(Objects.requireNonNull(departureDateTimeStr), formatter);
        this.arrivalTime = LocalDateTime.parse(Objects.requireNonNull(arrivalDateTimeStr), formatter);
    }
}
